package com.TPNexus;

import java.io.Serializable;
import java.util.ArrayList;

import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

import com.TPNexus.dao.ProductDAO;
import com.TPNexus.model.Product;

@Named
@SessionScoped
public class ProductPagesController implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Inject
	private ProductDAO productDAO;
	@Inject
	private Conversation conversation;

	// Constructor
	public ProductPagesController() {
		super();
	}

	@PostConstruct
	public void init() {
		nProduct = new Product();
		nProduct.setStatus(true);
	}

/*	public void initConversation() {
		if (conversation.isTransient()) {
			conversation.begin("new");
			conversation.begin("update");
		}
	}*/

	// load data
	public ArrayList<Product> getProductList() {
		return productDAO.getProductList();
	}

	// member variable and getter/setter
	// member variable-add new
	private Product nProduct;

	public Product getnProduct() {
		//if (conversation.isTransient())
		//	conversation.begin("new");
		return nProduct;
	}

	public void setnProduct(Product nProduct) {
		this.nProduct = nProduct;
	}

	// member variable-delete and detail
	private Product product;

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	// member variable-update
	private Product uProduct;

	public Product getuProduct() {
		//if (conversation.isTransient())
		//	conversation.begin("update");
		return uProduct;
	}

	public void setuProduct(Product uProduct) {
		this.uProduct = uProduct;
	}

	// Add a new product
	public String toAddNewProduct() {
		nProduct = new Product();
		nProduct.setStatus(true);

		return "AddNewProduct";
	}

	public String addToConfirmation() {
		return "NewProductConfirmation";
	}

	public String addNewProduct() {
		try {
			productDAO.addNewProduct(nProduct);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String snew = conversation.getId();
		System.out.println("snew=" + snew);
		//if (!conversation.isTransient())
			//conversation.end();
		//conversation.setTimeout(1000l);
		return "ProductList";
	}

	// product detail
	public String getProductDetail() {
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();
		Integer id = Integer.parseInt(request.getParameter("id"));
		product = productDAO.getProductDetail(id);
		return "ProductDetail";
	}

	// update product
	public String toUpdateProduct() {
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();
		Integer id = Integer.parseInt(request.getParameter("id"));
		uProduct = productDAO.getProductDetail(id);

		return "UpdateProduct";
	}

	public String updateToConfirmation() {
		return "UpdateProductConfirmation";
	}

	public String updateProduct() {
		productDAO.updateProduct(uProduct);
		String supdate = conversation.getId();
		System.out.println("supdate=" + supdate);
		//if (!conversation.isTransient())
			//conversation.end();
		//conversation.setTimeout(1000l);
		return "ProductList";
	}

	// delete
	public String deleteProduct() {
		HttpServletRequest request = (HttpServletRequest) FacesContext
				.getCurrentInstance().getExternalContext().getRequest();
		Integer id = Integer.parseInt(request.getParameter("id"));
		product = productDAO.getProductDetail(id);
		productDAO.deleteProduct(product);
		return "ProductList";
	}

	public void valueChanged(ValueChangeEvent event) {
		System.out.println("Line 65");
	}

}
